﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using UnityEditor;



public class MakePrefab : MonoBehaviour
{
    public GameObject go;
    public Material sm;

    [ButtonCallFunc]
    public bool Make;
    public void MakeMethod()
    {
        var prefabPath = Path.Combine(Application.dataPath, "Prefab");
        prefabPath = MyUtils.FullPathToUnityPath(prefabPath);

        var path = AssetDatabase.GetAssetPath(go);
        var dn = Path.GetDirectoryName(path);
        var dir = new DirectoryInfo(dn);
        var total  = dir.GetFiles("*.obj", SearchOption.AllDirectories);
        var c = 0;
        foreach (var f in total)
        {
            c++;
            var fn = Path.GetFileNameWithoutExtension(f.FullName);
            var obj = MyUtils.FullPathToUnityPath(f.FullName);
            var pp = Path.Combine(prefabPath, fn+".prefab");
            EditorUtility.DisplayProgressBar("Con", pp, c / total.Length * 1.0f);
            if (File.Exists(pp)) continue;
            Debug.Log("Convert:" + pp);
            var go = AssetDatabase.LoadAssetAtPath<GameObject>(obj);
            var newGo = GameObject.Instantiate<GameObject>(go);
            var mr = newGo.GetComponentInChildren<MeshRenderer>();
            mr.sharedMaterial = sm;
            PrefabUtility.CreatePrefab(pp, newGo);
            GameObject.DestroyImmediate(newGo);
        }
        EditorUtility.ClearProgressBar();
    }
}
